package com.zzl.springboot_mybatis_demo.configuration;

import com.zzl.springboot_mybatis_demo.filter.RequestFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

/**
 * description: 请求过滤器配置
 * @author 朱正磊
 * @date 2019-12-09 14:03:49
 */
@SuppressWarnings("ALL")
@Configuration
public class RequestFilterConfig {

    /**
     * description: 配置请求过滤器
     * @author 朱正磊
     * @date 2019-12-09 14:05:51
     * @return 返回过滤器Bean
     */
    @Bean
    public FilterRegistrationBean requestFilterBean() {
        // 通过FilterRegistrationBean实例设置优先级可以生效
        FilterRegistrationBean bean = new FilterRegistrationBean();
        // 注册自定义过滤器
        bean.setFilter(new RequestFilter());
        // 设置过滤器名称
        bean.setName("requestFilter");
        // 添加指定过滤路径(如果是"/*"则表示过滤所有的请求路径包括静态资源文件)
        List<String> urlPatterns = new ArrayList<>();
        urlPatterns.add("/api/*");
        bean.setUrlPatterns(urlPatterns);
        // 设置优先级，优先级越低越优先
        bean.setOrder(1);
        return bean;
    }

}
